package cn.hls.web.controller.dict;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hls.entity.ACL;
import cn.hls.entity.vo.Msg;
import cn.hls.service.ACLService;
import cn.hls.web.controller.base.BaseController;

/**
 *@fileName: ACLControll.java
 *@package: com.bangni.web.controller.dict
 *@Description: ACL Controller控制类 
 *@author: yinghui.zhang
 *@email: uuhui@163.com
 *@date: 2015-5-2
 */
@Controller
@RequestMapping("/acl")
public class ACLController extends BaseController<ACL> {
	@Resource(name="aclService")
	private ACLService aclService;
	/**
	 * 根据编号查询acl
	 * @param principalType 主体类型
	 * @param principalId 主体标识
	 * @param moduleId 模块编号
	 * @return List<ACL>
	 * @throws Exception 异常
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/searchAclRecord")
	@ResponseBody
	public List searchAclRecord(String principalType,
			Long principalId, Long moduleId) throws Exception {
		return aclService.searchAclRecord(getPrincipalType(principalType), principalId, moduleId);
	}
	/**
	 * 获取标识类型
	 * @param type 类型
	 * @return Integer
	 */
	private Integer getPrincipalType(String type){
		Integer principalType=null;
		 if(type.equalsIgnoreCase("person")){
			 principalType=ACL.TYPE_ROLE;
		 }else if(type.equalsIgnoreCase("org")){
			 principalType=ACL.TYPE_ORG;
		 }else{
			 principalType=ACL.TYPE_ROLE;
		 }
		 return principalType;
	}
	/**
	 * 单个CURD 授权
	 * @return　Msg
	 * @throws Exception 异常
	 */
	@RequestMapping("/addOrUpdatePermission")
	@ResponseBody
	public Msg addOrUpdatePermission(String principalType,Long principalId,
			Long moduleId,Integer permissionExt,boolean yes) throws Exception {
		 
		try {
			aclService.addOrUpdatePermission(getPrincipalType(principalType), principalId,moduleId, permissionExt, yes);
			msg.setStatus(200);
			msg.setInfo("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			msg.setStatus(201);
			msg.setInfo("操作失败");
		}
		 return msg;
	}
	
	/**
	 * 扩展权限
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addOrUpdateExtPermission")
	@ResponseBody
	public Msg addOrUpdateExtPermission(String principalType,Long principalId,
			Long moduleId,Integer permissionExt,String ex) throws Exception {
		try {
			aclService.addOrUpdateExtPermission(getPrincipalType(principalType), principalId,moduleId, ex);
			msg.setStatus(200);
			msg.setInfo("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			msg.setStatus(201);
			msg.setInfo("操作失败");
		}
		return msg;
	}
	
	/**
	 * 设定是否启用 : 机构，角色
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateEnable")
	@ResponseBody
	public Msg updateEnable(String principalType,Long principalId,
			Long moduleId,boolean yes) throws Exception {
		try {
			aclService.updateEnable(getPrincipalType(principalType), principalId,moduleId, yes);
			msg.setStatus(200);
			msg.setInfo("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			msg.setStatus(201);
			msg.setInfo("操作失败");
		}
		 return msg;
	}
	
	/**
	 * 设定用户状态
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateUserEnable")
	@ResponseBody
	public Msg updateUserEnable(Long principalId,
			Long moduleId,Integer state) throws Exception {
		try {
			aclService.addOrUpdateUserEnable(principalId,moduleId, state);
			msg.setStatus(200);
			msg.setInfo("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			msg.setStatus(201);
			msg.setInfo("操作失败");
		}
		 return msg;
	}
	
	/**
	 * 设置全选/反选
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateAll")
	@ResponseBody
	public Msg updateAll(String principalType,Long principalId,
			Long moduleId,boolean yes) throws Exception {
		boolean flag=false;
		 Integer type=getPrincipalType(principalType);
		  ACL acl = aclService.findACL(type, principalId, moduleId);
		  
			if(acl != null){
				if(yes) {
					 acl.setAclState(65535);
					 acl.setAclTriState(0); 
				} else {
					 acl.setAclState(0);
					 acl.setAclTriState(1); 
				}
				flag=aclService.update(acl);
			} else {
				acl = new ACL();
				acl.setPrincipalType(type);
				acl.setPrincipalId(principalId);
				acl.setModuleId(moduleId);
				acl.setAclState(65535);
				acl.setAclTriState(0); 
				flag=aclService.add(acl);
			}
			if(flag){
				msg.setStatus(200);
				msg.setInfo("操作成功");
			}else{
				msg.setStatus(201);
				msg.setInfo("操作失败");
			}
		return msg;
	}
	
}
